package com.example.human_resource_management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.EmployeeForumAdminDto;
import com.example.human_resource_management.entry.po.EmployeeForum;
import com.example.human_resource_management.service.EmployeeForumService;
import com.example.human_resource_management.mapper.EmployeeForumMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author Lenovo
* @description 针对表【employee_forum(员工论坛表)】的数据库操作Service实现
* @createDate 2025-06-09 14:53:50
*/
@Service
public class EmployeeForumServiceImpl extends ServiceImpl<EmployeeForumMapper, EmployeeForum>
    implements EmployeeForumService{

    @Autowired
    private EmployeeForumMapper employeeForumMapper;

    @Override
    public List<EmployeeForum> selectEmployeeForumList() {
        return this.list();
    }

    /*    @Override
        public EmployeeForum getEmployeeForumById(Long empId) {
            return employeeForumMapper.selectById(empId);
        }*/
    @Override
    public List<EmployeeForum> getEmployeeForumsByEmpId(Long empId) {
        return employeeForumMapper.selectList(
                new QueryWrapper<EmployeeForum>().eq("emp_id", empId)
        );
    }

    @Override
    public ResponseData<String> createEmployeeForum(EmployeeForum employeeForum) {
        employeeForum.setPostTime(LocalDateTime.now());
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到发帖人信息");
        }

        return save(employeeForum) ?
                ResponseData.ok("发帖成功") :
                ResponseData.warn("发帖失败");
    }

    @Override
    public ResponseData<String> deleteEmployeeForum(Long empId, Long postId) {
        int result = employeeForumMapper.deleteForumByEmpIdAndPostId(empId, postId);
        if (result > 0) {
            return ResponseData.ok("员工论坛帖子删除成功");
        } else {
            return ResponseData.warn("未找到该员工发布的此帖子或删除失败");
        }
    }


    @Override
    public List<EmployeeForumAdminDto> selectAllEmployeeForums() {
        return employeeForumMapper.selectAllEmployeeForums();
    }

    @Override
    public ResponseData<String> adminCreateEmployeeForum(EmployeeForum employeeForum) {
        boolean success = save(employeeForum);
        return success ? ResponseData.ok("新增论坛帖子成功") : ResponseData.warn("新增论坛帖子失败");
    }

    @Override
    public ResponseData<String> adminUpdateEmployeeForum(EmployeeForum employeeForum) {
        boolean success = updateById(employeeForum);
        return success ? ResponseData.ok("更新论坛帖子成功") : ResponseData.warn("更新论坛帖子失败");
    }

    @Override
    public ResponseData<String> adminDeleteEmployeeForum(Long postId) {
        boolean success = removeById(postId);
        return success ? ResponseData.ok("删除论坛帖子成功") : ResponseData.warn("删除论坛帖子失败");
    }
}




